using System;
using System.Collections.Generic;
using Csla;
using Ent.AccesoDatos;

namespace Ent.Inscripcion.Core.Core
{
    [Serializable]
    public class HorarioExamenLista :
      ReadOnlyListBase<HorarioExamenLista, HorarioExamenInfo>
    {
        #region Factory Methods

        public static HorarioExamenLista GetHorarioExamenLista(HorarioExamenCriterio criterio)
        {
            return DataPortal.Fetch<HorarioExamenLista>(criterio);
        }

        private HorarioExamenLista()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void DataPortal_Fetch(HorarioExamenCriterio criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            var parametros = new List<Parametro>
                                 {
                                     new Parametro("Nivel", criteria.NivelId),
                                     new Parametro("Dia", criteria.DiaId)
                                 };
            //object objectData = null;
            var datos = accesoDatos.EjecutarReader("ObtenerHorarioExamenes", parametros);
            while (datos.Datos.Read())
            {
                Add(HorarioExamenInfo.GetHorarioExamenInfo(datos));
            }
            if (!datos.Datos.IsClosed) datos.Datos.Close();
                
            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }


    [Serializable]
    public class HorarioExamenInfo : ReadOnlyBase<HorarioExamenInfo>
    {
        #region Business Methods

        private static PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(o => o.Id, "Id");
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
        }

        private static PropertyInfo<DateTime> HoraInicioProperty = RegisterProperty<DateTime>(o => o.HoraInicio, "Hora Inicio");
        public DateTime HoraInicio
        {
            get { return GetProperty(HoraInicioProperty); }
        }

        private static PropertyInfo<DateTime> HoraFinProperty = RegisterProperty<DateTime>(o => o.HoraFin, "Hora Fin");
        public DateTime HoraFin
        {
            get { return GetProperty(HoraFinProperty); }
        }

        private static PropertyInfo<string> AulaIdProperty = RegisterProperty<string>(o => o.AulaId, "Name");
        public string AulaId
        {
            get { return GetProperty(AulaIdProperty); }
        }

        private static PropertyInfo<string> NivelIdProperty = RegisterProperty<string>(o => o.NivelId, "Name");
        public string NivelId
        {
            get { return GetProperty(NivelIdProperty); }
        }

        private static PropertyInfo<string> DiaIdProperty = RegisterProperty<string>(o => o.DiaId, "Name");
        public string DiaId
        {
            get { return GetProperty(DiaIdProperty); }
        }

        private static PropertyInfo<string> DiaProperty = RegisterProperty<string>(o => o.Dia, "Name");
        public string Dia
        {
            get { return GetProperty(DiaProperty); }
        }

        private static PropertyInfo<string> NivelProperty = RegisterProperty<string>(o => o.Nivel, "Name");
        public string Nivel
        {
            get { return GetProperty(NivelProperty); }
        }

        private static PropertyInfo<string> AulaProperty = RegisterProperty<string>(o => o.Aula, "Name");
        public string Aula
        {
            get { return GetProperty(AulaProperty); }
        }


        private static PropertyInfo<int> EstadoProperty = RegisterProperty<int>(o => o.Estado, "Name");
        public int Estado
        {
            get { return GetProperty(EstadoProperty); }
        }

        private static PropertyInfo<int> CupoProperty = RegisterProperty<int>(o => o.Cupo, "Name");
        public int Cupo
        {
            get { return GetProperty(CupoProperty); }
        }

        private static PropertyInfo<string> BaseIdProperty = RegisterProperty<string>(o => o.BaseId, "Name");
        public string BaseId
        {
            get { return GetProperty(BaseIdProperty); }
        }

        public string Niveles
        {
            get; set;
        }

        #endregion

        #region Factory Methods

        internal static HorarioExamenInfo GetHorarioExamenInfo(object childData)
        {
            return DataPortal.FetchChild<HorarioExamenInfo>(childData);
        }

        private HorarioExamenInfo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(Reader datos)
        {
            LoadProperty(IdProperty, Guid.Parse(datos.Datos.GetString(0)));
            LoadProperty(HoraInicioProperty,DateTime.Parse( datos.Datos.GetString(1)));
            LoadProperty(HoraFinProperty, DateTime.Parse(datos.Datos.GetString(2)));
            LoadProperty(AulaIdProperty, datos.Datos.GetString(3));
            LoadProperty(NivelIdProperty, datos.Datos.GetString(4));
            LoadProperty(DiaIdProperty, datos.Datos.GetString(5));
            LoadProperty(AulaProperty, datos.Datos.GetString(6));
            LoadProperty(DiaProperty , datos.Datos.GetString(7));
            LoadProperty(NivelProperty, datos.Datos.GetString(8));
            LoadProperty(EstadoProperty,Convert.ToInt32( datos.Datos.GetInt32(9)));
            LoadProperty(CupoProperty, datos.Datos.GetInt32(10));
            LoadProperty(BaseIdProperty, datos.Datos.GetString(11));
            Niveles = string.Format("{0} de {1} a {2}", ReadProperty(DiaProperty), ReadProperty(HoraInicioProperty).ToShortTimeString(), ReadProperty(HoraFinProperty).ToShortTimeString());
        }

        #endregion
    }
}
